package com.blockmeta.bbs.immodule.widget.msgview;

import android.content.Context;
import android.text.TextUtils;
import com.blankj.utilcode.util.LogUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.blockmeta.bbs.baselibrary.net.callback.BaseResponseCallBack;
import com.blockmeta.bbs.baselibrary.utils.DimentionsUtil;
import com.blockmeta.bbs.baselibrary.utils.LoadPicUtil;
import com.blockmeta.bbs.baselibrary.utils.gson.GsonUtil;
import com.blockmeta.bbs.businesslibrary.util.AccountHelper;
import com.blockmeta.bbs.immodule.R;
import com.blockmeta.bbs.immodule.corebean.Impl.IMApplicationContext;
import com.blockmeta.bbs.immodule.dao.ChatMessage;
import com.blockmeta.bbs.immodule.dao.IMUserEntity;
import com.blockmeta.bbs.immodule.utils.IMUtils;
import com.blockmeta.bbs.immodule.utils.ImTimeUtils;
import com.chad.library.adapter.base.BaseViewHolder;

import org.json.JSONException;
import org.json.JSONObject;

public abstract class MsgView<T extends ChatMessage> implements IMsgView<T>{

    protected Context mContext;
    protected boolean needShowPbSending = true;

    public MsgView(Context context) {
        mContext = context;
    }


    @Override
    public void convert(BaseViewHolder helper, T item) {

    }



    @Override
    public void convert(BaseViewHolder helper, T item, T lastItem) {
        //处理时间显示逻辑！
        if (lastItem != null) {
            int lastDate = lastItem.getDate();
            if (item.getDate() - lastDate > 120) {
                helper.setGone(R.id.tv_time, true);
            } else {
                helper.setGone(R.id.tv_time, false);
            }
        } else {
            helper.setGone(R.id.tv_time, true);
        }
        TextView time = helper.getView(R.id.tv_time);
        time.setText(ImTimeUtils.SessionListTime(item.getDate()));
    }

    protected void bindCommonControl(BaseViewHolder helper, T item, T lastItem){
        View receiveContainer = helper.getView(R.id.left_container);
        View sendContainer = helper.getView(R.id.right_container);

        //发送消息！
        if (item.getType() == ChatMessage.TYPE_SEND) {
            receiveContainer.setVisibility(View.GONE);
            sendContainer.setVisibility(View.VISIBLE);
            ImageView icon = helper.getView(R.id.iv_right_avatar);
            LoadPicUtil.loadWithPicasso(icon, AccountHelper.getUserPhonePOJO().getLogo());

            helper.addOnClickListener(R.id.send_tx_fail);

            if (item.getStatus() == ChatMessage.STATUS_SENDING) {
                helper.setGone(R.id.send_tx_pb, needShowPbSending);
                helper.setGone(R.id.send_tx_fail, false);
            } else if (item.getStatus() >= ChatMessage.STATUS_FAIL_FRIEND_SHIELD || item.getStatus() == ChatMessage.STATUS_UNKNOWN) {
                helper.setGone(R.id.send_tx_pb, false);
                helper.setGone(R.id.send_tx_fail, true);
            } else {
                helper.setGone(R.id.send_tx_pb, false);
                helper.setGone(R.id.send_tx_fail, false);
            }
            helper.addOnLongClickListener(R.id.tv_right_content);
        } else {
            //接收消息
            receiveContainer.setVisibility(View.VISIBLE);
            sendContainer.setVisibility(View.GONE);

            final ImageView icon = helper.getView(R.id.iv_left_avatar);
            final TextView name = helper.getView(R.id.tv_send_name);

            bindFromUserInfo(helper, item, icon, name);

            helper.addOnClickListener(R.id.iv_left_avatar);
            helper.addOnLongClickListener(R.id.iv_left_avatar);
            helper.addOnLongClickListener(R.id.tv_left_content);
        }
    }


    protected void bindFromUserInfo(final BaseViewHolder helper, final ChatMessage item, final ImageView icon, final TextView name) {
        IMApplicationContext.getUserDomainService().getUser(item.getFromId(), false, new BaseResponseCallBack<String, IMUserEntity>() {
            @Override
            public void fail(String msg) {
            }

            @Override
            public void success(IMUserEntity iMUserEntity) {
                LoadPicUtil.loadWithPicasso(icon, iMUserEntity.getAvatarPath());

                if(item.getMsgType() == ChatMessage.MSG_TYPE_UCG){
                    helper.setGone(R.id.layout_user_info, true);
                    TextView contactGrade = helper.getView(R.id.contacts_grade);
                    contactGrade.setText(IMUtils.UserLevel.getUserLevel(iMUserEntity.getUserLevel()).getValue());
                    boolean isFriend = IMApplicationContext.getUserDomainService().isFriend(iMUserEntity.getUserId());
                    TextView contactFriend = helper.getView(R.id.contacts_friend);
                    contactFriend.setText(isFriend ? "(好友)" : "");
                    //获取圈子备注名称
                    name.setText(IMApplicationContext.getCircleDomainService().getCircleRemarkName(iMUserEntity, item.getChatGroupId()));
                }else {
                    String showName = TextUtils.isEmpty(iMUserEntity.getRemarkName()) ? iMUserEntity.getName() : iMUserEntity.getRemarkName();
                    helper.setGone(R.id.layout_user_info, false);
                    name.setText(showName);
                }
            }
        });
    }


    protected void setImageSize(ImageView sendPic, ChatMessage item) {
        String fileParam = item.getFileParam();
        int maxWidth = (int) DimentionsUtil.dp2px(120.0f);
        int maxHeight = (int) DimentionsUtil.dp2px(200.0f);
        if(TextUtils.isEmpty(fileParam)){
            sendPic.getLayoutParams().width = (int) (maxHeight);
            sendPic.getLayoutParams().height = (int) (maxHeight);
            sendPic.requestLayout();
            return;
        }
        try {
            JSONObject fileParamJson = new JSONObject(item.getFileParam());
            int realWidth = fileParamJson.getInt("w");
            int realHeight = fileParamJson.getInt("h");

            float scale = Math.max(1.f * realWidth / maxWidth,
                    1.f *realHeight / maxHeight);
            if (scale <= 1)
            {
                sendPic.getLayoutParams().width = realWidth;
                sendPic.getLayoutParams().height = realHeight;
            }
            else
            {
                sendPic.getLayoutParams().width = (int) (realWidth / scale);
                sendPic.getLayoutParams().height = (int) (realHeight / scale);
                LogUtils.i("author" + "setImageSize" + sendPic.getLayoutParams().width + " "+ sendPic.getLayoutParams().height);
            }

            sendPic.requestLayout();
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }
    protected <E> E getContentBean(T item, Class<E> obj) {
        return GsonUtil.buildGson().fromJson(item.getContent(), obj);
    }
}
